// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"username": {
		"rules": [{
				"required": true,
			},
			{
				"format": "string"
			},
			{
				"minLength": 2
			}
		],
		"label": "用户名"
	},
	"comment": {
		"rules": [{
			"format": "string"
		}]
	},
	"gender": {
		"rules": [{
				"format": "int"
			},
			{
				"range": [{
						"text": "未知",
						"value": 0
					},
					{
						"text": "男",
						"value": 1
					},
					{
						"text": "女",
						"value": 2
					}
				]
			}
		],
		"defaultValue": 0,
		"label": "性别"
	},
	"password": {
		"rules": [{
				"required": true,
			},
			{
				"format": "password"
			},
			{
				"minLength": 6
			}
		],
		"label": "密码"
	},
	"mobile": {
		"rules": [{
				"format": "string"
			},
			{
				"pattern": "^\\+?[0-9-]{3,20}$"
			}
		],
		"label": "手机号码"
	},
	"status": {
		"rules": [{
				"format": "int"
			},
			{
				"range": [{
						"text": "正常",
						"value": 0
					},
					{
						"text": "禁用",
						"value": 1
					},
					{
						"text": "审核中",
						"value": 2
					},
					{
						"text": "审核拒绝",
						"value": 3
					}
				]
			}
		],
		"defaultValue": 0,
		"label": "用户状态"
	},
	"mechanism_id": {
		"rules": [{
			"format": "string"
		}],
		"label": "机构"
	},
	"email": {
		"rules": [{
				"format": "string"
			},
			{
				"format": "email"
			}
		],
		"label": "邮箱"
	},
	"role": {
		"rules": [{
			"format": "array"
		}],
		"label": "角色"
	},
	"dcloud_appid": {
		"rules": [{
			"format": "array"
		}],
		"label": "应用列表"
	},
	"register_date": {
		"rules": [{
			"format": "timestamp"
		}]
	},
	"type_subject_id": {
		"rules": [{
			"format": "string"
		}],
		"label": "科目"
	},

	"type_drive_id": {
		"rules": [{
			"format": "string"
		}],
		"label": "车型"
	},



	"type_cert_id": {
		"rules": [{
			"format": "string"
		}],
		"label": "考试类型"
	},



	"id_card": {
		"rules": [{
			"format": "string"
		}],
		"label": "身份证"
	},
	"student_name": {
		"rules": [{
			"format": "string"
		}],
		"label": "姓名"
	},

	"subject_page_ids": {
		"rules": [{
				"format": "array",

			},
			{
				"arrayType": "string"
			}
		],
		"label": "试卷"
	},
	"recommender": {
		"rules": [{
			"format": "string"
		}],
		"label": "推荐人"
	},



	"recommender_mobile": {
		"rules": [{
			"format": "string"
		}],
		"label": "推荐人手机"
	},



	"tuition": {
		"rules": [{
			"format": "double"
		}],
		"label": "学费"
	},

	"test_times": {
		"rules": [{
			"format": "timestamp"
		}],
		"label": "考试时间"
	},

	"fee_status": {
		"rules": [{
				"format": "int"
			},
			{
				"range": [{
						"text": "未缴费",
						"value": 0
					}, {
						"text": "已缴费",
						"value": 1
					}, {
						"text": "已毕业",
						"value": 2
					},
					{
						"text": "已过期",
						"value": 3
					}, {
						"text": "禁用",
						"value": 4
					}
				]
			}
		],
		"defaultValue": 0,
		"label": "学员状态"
	},

	"use_times": {
		"rules": [{
			"format": "int"
		}],
		"label": "使用次数"
	},

}

const fee_statue_text = {
	text: {
		"0": "未缴费",
		"1": "已缴费",
		"2": "已毕业",
		"3": "已过期",
		"4": "禁用",
	}

}


const enumConverter = {
	"status_valuetotext": {
		"0": "正常",
		"1": "禁用",
		"2": "审核中",
		"3": "审核拒绝"
	}
}

const enumConverterGender = {
	"status_valuetotext": {
		"0": "未知",
		"1": "男",
		"2": "女",
	}
}
const enumGenderList = {
	"getnder_list": [{
			value: 0,
			text: "未知"
		},
		{
			value: 1,
			text: "男"
		},
		{
			value: 2,
			text: "女"
		}
	]
}

const enumConverterStudent = {
	"status_valuetotext": [{
			value: 0,
			text: "未缴费"
		},
		{
			value: 1,
			text: "已缴费"
		},
		{
			value: 2,
			text: "已毕业"
		},
		{
			value: 3,
			text: "已过期"
		},
		{
			value: 4,
			text: "禁用"
		}
	]


}


function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	fee_statue_text,
	enumGenderList,
	validator,
	enumConverter,
	filterToWhere,
	enumConverterStudent,
	enumConverterGender
}
